package com.fastloan.safe.mgt.root.service.impl;

import com.fastloan.safe.core.util.ManagerKit;
import com.fastloan.safe.mgt.root.dao.CmDefRoleMapper;
import com.fastloan.safe.mgt.root.model.CmDefFun;
import com.fastloan.safe.mgt.root.model.CmDefRole;
import com.fastloan.safe.mgt.root.model.CmDefRoleFun;
import com.fastloan.safe.mgt.root.service.CmDefFunService;
import com.fastloan.safe.mgt.root.service.CmDefRoleFunService;
import com.fastloan.safe.mgt.root.service.CmDefRoleService;
import com.fastloan.safe.mgt.user.model.CmuUser;
import com.fastloan.safe.mgt.user.service.CmuUserService;
import com.kawins.base.BaseMapper;
import com.kawins.base.baseabs.BaseServiceImpl;
import com.kawins.shiro.temp.util.ShiroKit;
import com.kawins.util.colle.ListUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.*;

@Service
public class CmDefRoleServiceImpl extends BaseServiceImpl<CmDefRole> implements CmDefRoleService {

    @Autowired
    private CmDefRoleMapper defRoleMapper;

    @Autowired
    private CmDefFunService defFunService;

    @Autowired
    private CmDefRoleFunService defRoleFunService;

    @Autowired
    private CmuUserService userService;

    @Override
    protected BaseMapper<CmDefRole> buildDao() {
        return defRoleMapper;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertWithFun(CmDefRole cmDefRole, String[] defFunIds) {
        log.debug("新增默认角色:" + cmDefRole);
        cmDefRole.setCreateBy(ManagerKit.getUser().getId());
        cmDefRole.setCreateTime(new Date());
        insertSelective(cmDefRole);
        bachInsertRoleFun(cmDefRole.getId(), defFunIds);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateWithFun(CmDefRole cmDefRole, String[] defFunIds) {
        CmDefRole defRole = selectByPrimaryKey(cmDefRole.getId());
        log.debug("更新默认角色:" + cmDefRole);
        cmDefRole.setCreateBy(null);
        cmDefRole.setCreateTime(null);
        cmDefRole.setIsAdmin(null);
        updateByPrimaryKeySelective(cmDefRole);
        defRoleFunService.delete(new CmDefRoleFun().setDefRoleId(cmDefRole.getId()));
        bachInsertRoleFun(cmDefRole.getId(), defFunIds);
        if (defRole.getIsAdmin().equals(1)) {
            clearAuth(cmDefRole.getId());
        }
    }

    private void clearAuth(String roleId) {
        CmuUser cmuUser = new CmuUser();
        cmuUser.setRootRoleId(roleId);
        List<CmuUser> select = userService.select(cmuUser);
        Set<Object> objects = ListUtil.listField2Set("username", select);
        for (Object object : objects) {
            ShiroKit.clearAuthorizationInfo(object.toString());
        }
    }

    private void bachInsertRoleFun(String defRoleId, String[] defFunIds) {
        if (defFunIds != null && defFunIds.length > 0) {

            Set<String> set = new HashSet<>(0);
            for (String defFunId : defFunIds) {
                set.add(defFunId);
                CmDefFun defFun = defFunService.selectByPrimaryKey(defFunId);
                while (true) {
                    if (defFun != null) {
                        set.add(defFun.getId());
                    }
                    if (defFun != null && StringUtils.isNotEmpty(defFun.getRelyOnId())) {
                        defFun = defFunService.selectByPrimaryKey(defFun.getRelyOnId());
                    } else {
                        break;
                    }
                }
            }
            List<CmDefRoleFun> list = new ArrayList<>();
            for (String funId : set) {
                list.add(new CmDefRoleFun().setDefRoleId(defRoleId).setDefFunId(funId));
            }
            defRoleFunService.insertList(list);
        }
    }
}
